﻿#region Copyright (c) 2013-05, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

#if TESTS

using System;
using System.Collections.Concurrent;
using System.Linq;
using NUnit.Framework;


namespace Amarok.Agents
{
	[TestFixture]
	public sealed class Test_Agent_Case67
	{
		private class MessageA : Message
		{
			public String Key;
		}

		private class MessageB : Message
		{
			public String Key;
		}

		private class MessageC : Message
		{
			public String Key;
		}

		private class MessageD : Message
		{
			public String Key;
		}


		private class TestAgent : Agent
		{
			public readonly ConcurrentQueue<String> Queue = new ConcurrentQueue<String>();


			public TestAgent(AgentEnvironment environment, AgentOptions options)
				: base(environment, options)
			{
			}

			[MessageHandler, ExclusiveExecution, MaximumParallelism]
			private void _HandleMessageA(MessageA message)
			{
				Queue.Enqueue(message.Key);
			}
			[MessageHandler, ExclusiveExecution, MaximumParallelism]
			private void _HandleMessageB(MessageB message)
			{
				Queue.Enqueue(message.Key);
			}

			[MessageHandler, ConcurrentExecution, OrderedProcessing]
			private void _HandleMessageC(MessageC message)
			{
				Queue.Enqueue(message.Key);
			}
			[MessageHandler, ConcurrentExecution, OrderedProcessing]
			private void _HandleMessageD(MessageD message)
			{
				Queue.Enqueue(message.Key);
			}
		}


		// --- TESTS ---


		[Test]
		public void Test_Case67_ExclusiveHandler()
		{
			var environment = new AgentEnvironment();
			var agent = new TestAgent(environment, AgentOptions.Empty);
			var agentInterface = (IAgent)agent;

			for (Int32 i = 0; i < 1000; i++)
			{
				agentInterface.Post(new MessageA()
				{
					Key = "A" + i.ToString()
				});
			}

			Helper.WaitForCondition(5000, 10, () => agent.Queue.Count == 1000);
			Assert.That(agent.Queue.Count, Is.EqualTo(1000));

			// A0 A1 A2 A3 ...
			Console.WriteLine("Test_Case67_ExclusiveHandler");
			Console.WriteLine(agent.Queue.ToArray().AsString(" "));

			var enumerator = agent.Queue.GetEnumerator();
			for (Int32 i = 0; i < 1000; i++)
			{
				Assert.That(enumerator.MoveNext(), Is.True);
				Assert.That(enumerator.Current, Is.EqualTo("A" + i.ToString()));
			}
		}

		[Test]
		public void Test_Case67_ConcurrentHandlerWithOrderedProcessing()
		{
			var environment = new AgentEnvironment();
			var agent = new TestAgent(environment, AgentOptions.Empty);
			var agentInterface = (IAgent)agent;

			for (Int32 i = 0; i < 1000; i++)
			{
				agentInterface.Post(new MessageC()
				{
					Key = "C" + i.ToString()
				});
			}

			Helper.WaitForCondition(5000, 10, () => agent.Queue.Count == 1000);
			Assert.That(agent.Queue.Count, Is.EqualTo(1000));

			// C0 C1 C2 C3 ...
			Console.WriteLine("Test_Case67_ConcurrentHandlerWithOrderedProcessing");
			Console.WriteLine(agent.Queue.ToArray().AsString(" "));

			var enumerator = agent.Queue.GetEnumerator();
			for (Int32 i = 0; i < 1000; i++)
			{
				Assert.That(enumerator.MoveNext(), Is.True);
				Assert.That(enumerator.Current, Is.EqualTo("C" + i.ToString()));
			}
		}


		[Test]
		public void Test_Case67_Multiple_ExclusiveHandler()
		{
			var environment = new AgentEnvironment();
			var agent = new TestAgent(environment, AgentOptions.Empty);
			var agentInterface = (IAgent)agent;

			for (Int32 i = 0; i < 500; i++)
			{
				agentInterface.Post(new MessageA()
				{
					Key = "A" + i.ToString()
				});
				agentInterface.Post(new MessageB()
				{
					Key = "B" + i.ToString()
				});
			}

			Helper.WaitForCondition(5000, 10, () => agent.Queue.Count == 1000);
			Assert.That(agent.Queue.Count, Is.EqualTo(1000));

			// A0 B0  A1 B1  A2 B2 ...
			Console.WriteLine("Test_Case67_Multiple_ExclusiveHandler");
			Console.WriteLine(agent.Queue.ToArray().AsString(" "));

			var enumerator = agent.Queue.GetEnumerator();
			for (Int32 i = 0; i < 500; i++)
			{
				Assert.That(enumerator.MoveNext(), Is.True);
				Assert.That(enumerator.Current, Is.EqualTo("A" + i.ToString()));

				Assert.That(enumerator.MoveNext(), Is.True);
				Assert.That(enumerator.Current, Is.EqualTo("B" + i.ToString()));
			}
		}

		[Test]
		public void Test_Case67_Multiple_ConcurrentHandlerWithOrderedProcessing()
		{
			var environment = new AgentEnvironment();
			var agent = new TestAgent(environment, AgentOptions.Empty);
			var agentInterface = (IAgent)agent;

			for (Int32 i = 0; i < 500; i++)
			{
				agentInterface.Post(new MessageC()
				{
					Key = "C" + i.ToString()
				});
				agentInterface.Post(new MessageD()
				{
					Key = "D" + i.ToString()
				});
			}

			Helper.WaitForCondition(5000, 10, () => agent.Queue.Count == 1000);
			Assert.That(agent.Queue.Count, Is.EqualTo(1000));

			// C0 C1 C2 C3...  D0 D1 D2 D3...
			Console.WriteLine("Test_Case67_Multiple_ConcurrentHandlerWithOrderedProcessing");
			Console.WriteLine(agent.Queue.ToArray().AsString(" "));

			var cMessages = agent.Queue.ToArray().Where(m => m.StartsWith("C"));
			var dMessages = agent.Queue.ToArray().Where(m => m.StartsWith("D"));

			var enumerator = cMessages.GetEnumerator();
			for (Int32 i = 0; i < 500; i++)
			{
				Assert.That(enumerator.MoveNext(), Is.True);
				Assert.That(enumerator.Current, Is.EqualTo("C" + i.ToString()));
			}

			enumerator = dMessages.GetEnumerator();
			for (Int32 i = 0; i < 500; i++)
			{
				Assert.That(enumerator.MoveNext(), Is.True);
				Assert.That(enumerator.Current, Is.EqualTo("D" + i.ToString()));
			}
		}


		[Test, Ignore]
		public void Test_Case67_ExclusiveHandler_And_ConcurrentHandlerWithOrderedProcessing()
		{
			var environment = new AgentEnvironment();
			var agent = new TestAgent(environment, AgentOptions.Empty);
			var agentInterface = (IAgent)agent;

			for (Int32 i = 0; i < 500; i++)
			{
				agentInterface.Post(new MessageA()
				{
					Key = "A" + i.ToString()
				});
				agentInterface.Post(new MessageC()
				{
					Key = "C" + i.ToString()
				});
			}

			Helper.WaitForCondition(5000, 10, () => agent.Queue.Count == 1000);
			Assert.That(agent.Queue.Count, Is.EqualTo(1000));

			// A0 A1 A2... A499 C1 C2...
			//  -> exclusive handler has priority; this behavior is not really intended but can't be changed easily
			Console.WriteLine("Test_Case67_ExclusiveHandler_And_ConcurrentHandlerWithOrderedProcessing");
			Console.WriteLine(agent.Queue.ToArray().AsString(" "));

			var enumerator = agent.Queue.GetEnumerator();
			for (Int32 i = 0; i < 500; i++)
			{
				Assert.That(enumerator.MoveNext(), Is.True);
				Assert.That(enumerator.Current, Is.EqualTo("A" + i.ToString()));
			}

			for (Int32 i = 0; i < 500; i++)
			{
				Assert.That(enumerator.MoveNext(), Is.True);
				Assert.That(enumerator.Current, Is.EqualTo("C" + i.ToString()));
			}
		}

	}
}

#endif
